#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import datetime,re
def week(year, month, day):
    first_week_month = datetime.datetime(year, month, 1).isocalendar()[1]
    if month == 1 and first_week_month > 10:
        first_week_month = 0
    user_date = datetime.datetime(year, month, day).isocalendar()[1]
    if month == 1 and user_date > 10:
        user_date = 0
    return user_date - first_week_month


#ss = [{'name':'All','id':'61,59,52','cc':[61,62]}]

#print len('73.69')

#cc = ['1.1','N/A','1.2']

#cc = sorted(cc)

#print cc
"""
try:
    ss = 0 / 0
except Exception,e:
    print e
"""


pids_list = []
pids_str = ""

for pids_list_value in pids_list:
    pids_str += "'%s'," % pids_list_value

pids = pids_str.rstrip(",")

if pids == "":
    pids = "'123'"

#print pids



Pad_app = [{'name':'PPTV网络电视 HD','id':'405667771','img':'405667771.jpg'},{'name':'优酷HD','id':'394075284.jpg','img':'405667771.jpg'},{'name':'乐视影视','id':'412395632','img':'412395632.jpg'},{'name':'搜狐视频 HD','id':'414430589','img':'414430589.jpg'},{'name':'PPS影音','id':'445375097','img':'445375097.jpg'},{'name':'奇艺高清影视','id':'409563112','img':'409563112.jpg'},{'name':'迅雷看看 HD','id':'396120024','img':'396120024.jpg'},{'name':'风行','id':'493899455','img':'493899455.jpg'},{'name':'土豆网 HD','id':'395893124','img':'395893124.jpg'},{'name':'腾讯视频HD','id':'407925512','img':'407925512.jpg'}]


Phone_app = [{'name':'优酷HD','id':'394075284.jpg','img':'405667771.jpg'},{'name':'乐视影视','id':'412395632','img':'412395632.jpg'},{'name':'搜狐视频 HD','id':'414430589','img':'414430589.jpg'},{'name':'PPS影音','id':'445375097','img':'445375097.jpg'},{'name':'奇艺高清影视','id':'409563112','img':'409563112.jpg'},{'name':'迅雷看看 HD','id':'396120024','img':'396120024.jpg'},{'name':'土豆网 HD','id':'395898626','img':'395898626.jpg'},{'name':'腾讯视频HD','id':'407925512','img':'407925512.jpg'}]

print datetime.datetime.now().strftime('%Y%m%d')
"""
_yesday = datetime.datetime.today() + datetime.timedelta(-1)

_des = _yesday.strftime('%Y%m%d')

print _des
"""
list1 = [1,2]
list2 = [2]

g = len(set(list1) & set(list2))

l1 = ['20120801','20120801','20120801','20120802','20120803','20120803','20120804']  
l2 = set(l1)  
print l2  

s2 = [['-1', 'f00026', '1', '1', '0'], ['-1', 'f00024', '1', '1', '0'], ['-1', '', '1', '1', '0'], ['wap', 'f00025', '1', '1', '0']]

s1={'f00026': {'-1': 0, 'sm': 0, 'wap': 0}, 'f00024': {'-1': 0, 'sm': 0, 'wap': 1}, 'f00025': {'-1': 0, 'sm': 0, 'wap': 0}}

"""
d1 = {'f00026':'1','f00025':'2','f00024':'3'}
print s1


for i in s2:
    print i,i[0]
    #print s2[i][0]
    if i[1] in d1:
        if i[0] == '-1':
            try:
                s1[i[1]]['-1'] = 3
            except:
                pass
    
print s1
"""
'''
import calendar
start_day,end_day = calendar.monthrange(2012,9)

print end_day
'''
L = "1,2,3"
#s = L.split(',')
#print type(s[0])
'''
import glob
listss = glob.glob('/tmp/*.notfinished')
if len(listss) != 0:
    chcc = listss[0]
    chcs = chcc.replace("/tmp/","").replace(".notfinished","")
    ccdate = datetime.datetime.strptime(chcs,'%Y%m%d')
    et_time = ccdate + datetime.timedelta(-1)
    st_datestr = et_time.strftime('%Y%m%d')
    print st_datestr
'''

testdate = datetime.datetime(2012,8,5)
weekofmonth = (testdate.day+7-1)/7
#print int(weekofmonth)

import urllib2
'''
url = "http://123.123"
_req = urllib2.Request(url)
opener = urllib2.urlopen(_req)
content = opener.read()

print opener.code,content
'''


'''
fails = 0
while True:
    try:
        print fails
        if fails >= 3:
            page = 'kong'
            break
        f = urllib2.urlopen("http://123.123",timeout=20)
        page = f.read()
    except:
        fails += 1
    else:
        break


print page

import hashlib
password = 'stat123456'
m = hashlib.md5(password)
m.digest()
print m.hexdigest()
'''

import time
'''
fails = 0
while True:
    try:
        print fails
        if fails >= 3:
            page = 'kong'
            break
        f = urllib2.urlopen("123",timeout=20)
        page = f.read()
    except:
        fails += 1
    else:
        break
'''

'''
fails = 0
while True:
    try:
        print fails,'chaoshi'
        if fails >= 2:
            result['code'] = 200
            result['msg'] = 'error'
            result['flag'] = 0
            result['content'] = ''
            break
        f = urllib2.urlopen('',timeout=20)
        page = f.read()
        result['code'] = 200
        result['msg'] = 'good'
        result['flag'] = 1
        result['content'] = page
    except:
        fails += 1
    else:
        break

'''
'''
_today = datetime.datetime.today()
_datetimet = _today.strftime('%Y%m%d')

print _datetimet

_yesday = datetime.datetime.today() + datetime.timedelta(-1)
_datetime = _yesday.strftime('%Y-%m-%d')
fails = 0
while True:
    try:
        print fails
        if fails >= 3:
            page = 'kong'
            break
        f = urllib2.urlopen("http://dev.gfan.com/Aspx/DevApp/down4dev.aspx?pid=149518",timeout=20)
        page = f.read()
    except:
        fails += 1
    else:
        break
patt = re.compile("<td>%s</td><td>(.+?)</td>" % _datetime)
try: 
    rihous = re.findall(patt,page)
    rihou = rihous[0]
except:
    rihou = 0
print rihou 
'''

riqis = "20121112"
zhou_now = datetime.datetime.strptime(riqis,'%Y%m%d')
_pickday = zhou_now.weekday()

yy = riqis[0:4]
mm = riqis[4:6]
dd = riqis[6:]
from datetime import date
from datetime import timedelta
start = date(year=int(yy),month=int(mm),day=int(dd))
if _pickday != 6:
    start_day = start - timedelta(days=start.weekday()) + timedelta(days=0, weeks=-1)
    end_day = start - timedelta(days=start.weekday()) + timedelta(days=6, weeks=-1)
else:
    start_day = start - timedelta(days=start.weekday()) + timedelta(days=0, weeks=0)
    end_day = start - timedelta(days=start.weekday()) + timedelta(days=6, weeks=0)


start_day = start_day.strftime('%Y%m%d')
end_day = end_day.strftime('%Y%m%d')

import datetime
today = datetime.datetime.strptime("20121014",'%Y%m%d')
first = datetime.date(day=1, month=today.month, year=today.year)
lastMonth = first - datetime.timedelta(days=1)
print lastMonth.strftime("%Y%m")

str='123'
print str[0:1]

